clear
do "$scriptsdir/uk_preparecdf.do"

clear
do "$scriptsdir/uk_prepareincome.do"

clear
use "$gendatadir/uk_incomeprocessed.dta"


//// Scatter plot of mean and t5 growth rates
// Do this before merging in the BES cumulative data file as this will remove non-election years
gen inc_ll_95_g1_100 =  inc_ll_95_g1 * 100
gen inc_mean_g1_100 = inc_mean_g1 * 100
twoway (function y=x, range(-10 10)) (scatter inc_ll_95_g1_100 inc_mean_g1_100 if election_date == ., msymbol(oh) yline(0, lstyle(dot)) xline(0, lstyle(dot))) (scatter inc_ll_95_g1_100 inc_mean_g1_100 if election_date != ., msymbol(o) mcolor(gs4)), scheme(s1mono) xtitle("Mean income growth (%)") ytitle("Top-5% income growth (%)") xlabel(-10(4)10) ylabel(-10(4)10) legend(off)
graph export "$imagedir/uk_scatter_mean_t5.pdf", replace
window manage close graph


cor inc_ll_95_g1_100 inc_mean_g1_100
local rho =  string(r(rho), "%9.2fc")
capture {
  file open  rhofile using "$texdir/uk_rho_mean_t5.tex", write replace
  file write rhofile "`rho'"
  file close rhofile
}
cor inc_ll_95_g1_100 inc_mean_g1_100 if election_date != .
local rho =  string(r(rho), "%9.2fc")
capture {
  file open  rhofile using "$texdir/uk_rho_mean_t5_eyears.tex", write replace
  file write rhofile "`rho'"
  file close rhofile
}


// We use only the first of the 1974 elections, which has year=19741 in "britain cdf.dta"
replace year = 19741 if year == 1974


gen            share5_x_T5g1 =	share5 * inc_ll_95_g1
label variable share5			"$ Share^{T5}_t $"
label variable share5_x_T5g1	"$ Share^{T5}_{t} \times Growth^{T5}_{t} $"


merge 1:m year using "$gendatadir/uk_cdf.dta", nogenerate

sort year

// Create weights that weight each election equally
bys election_date: egen weight_sum = total(wt)
gen weight_eqelec = 10000*wt/weight_sum 

gen logtenure = log(incumbent_tenure)


label variable vote_ce				"$ VoteIncumbent_{i,t} $"
label variable inc_mean_g1			"$ Growth^{M}_{t} $"
label variable inc_median_g1		"$ Growth^{P50}_{t} $"
label variable inc_meanb80_g1		"$ Growth^{B80M}_{t} $"
label variable incumbent_tenure		"$ Tenure_{t} $"
label variable logtenure			"$ \log Tenure_{t} $"
label variable pid_ce 				"$ ProPartyID_{i,t} $"
label variable inc_ul_quin1_g1		"$ Growth^{P20}_{t} $"
label variable inc_ul_quin2_g1		"$ Growth^{P40}_{t} $"
label variable inc_ul_quin3_g1		"$ Growth^{P60}_{t} $"
label variable inc_ul_quin4_g1		"$ Growth^{P80}_{t} $"
label variable wtid_incgrowthb90	"$ Growth^{B90M}_{t} $"
label variable inc_ll_95_g1			"$ Growth^{P95}_{t} $"


label variable unioninfluenced		"$ UnionInfl_{i,t} $"
label variable intissuenew			"$ IntIssues_{i,t} $"



// Drop abstainers
keep if turnout == 1


global estout_options          label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0) labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) p(fmt(2)) " ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_w_se     label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0)     labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_ols_w_se label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2 N xtercsamp, fmt(2 0 0) labels("$ R^2 $" "N" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )


//// Estimate some aggregated models by ols - so excluding PID.
preserve
collapse (mean) vote_ce incumbent_tenure logtenure pid_ce inc_mean_g1 inc_ll_95_g1 share5 [pweight=weight_eqelec], by(year)
reg vote_ce logtenure inc_mean_g1 inc_ll_95_g1
reg vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1
reg pid_ce logtenure inc_mean_g1 inc_ll_95_g1
estadd local xtercsamp = "All"
estimates store PIDOLSAll
restore

preserve
collapse (mean) vote_ce incumbent_tenure logtenure pid_ce inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, by(year)
reg vote_ce logtenure inc_mean_g1 inc_ll_95_g1
reg vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1
reg pid_ce logtenure inc_mean_g1 inc_ll_95_g1
estadd local xtercsamp = "Low"
estimates store PIDOLSLow
restore

preserve
collapse (mean) vote_ce incumbent_tenure logtenure pid_ce inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 1, by(year)
reg vote_ce logtenure inc_mean_g1 inc_ll_95_g1
reg vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1
reg pid_ce logtenure inc_mean_g1 inc_ll_95_g1
estadd local xtercsamp = "Mid"
estimates store PIDOLSMid
restore

estout  PIDOLSAll PIDOLSLow PIDOLSMid		using "$texdir/uk_pid.tex",	$estout_options_ols_w_se order(logtenure inc_mean_g1 inc_ll_95_g1)


//// Section of models that we have decided to include in the main text of CBEVComparative


probit vote_ce logtenure        pid_ce inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenure
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLow
probit vote_ce logtenure pid_ce inc_ul_quin1_g1   inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ul_quin1_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1Low
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMid
probit vote_ce logtenure pid_ce inc_median_g1   inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3Mid
probit vote_ce logtenure pid_ce inc_mean_g1 inc_median_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3Mid

estout  BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid		using "$texdir/uk_cbevcomp_tableA.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_ul_quin1_g1 inc_median_g1 inc_ll_95_g1)


// An identifier so we can summarize based on year not individual-level sample size
bys year: gen firstob = 1 if _n == 1
replace firstob = 0 if firstob == .


// Some code to generate predicted probabilities of incumbent vote to illustrate magnitudes in the paper.
estimates restore PreferredQ3Mid
summ inc_ll_95_g1 if firstob == 1
margins, at(inc_ll_95_g1=(0.024 0.076)) asobserved




//// Section of models that we present in supplementary material

probit vote_ce incumbent_tenure pid_ce inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store Bartels
probit vote_ce logtenure        pid_ce             inc_ll_95_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoMean
probit vote_ce logtenure        pid_ce inc_mean_g1              [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoT5

probit vote_ce logtenure pid_ce             inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowNoMean
probit vote_ce logtenure pid_ce inc_mean_g1              [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowNoT5

*probit vote_ce logtenure pid_ce                   inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
*estadd local xtercsamp = "Low"
*estimates store PreferredQ1LowNoQ1
probit vote_ce logtenure pid_ce inc_ul_quin1_g1                [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1LowNoT5

estout  Bartels BartelsLogtenure								using "$texdir/uk_cbevcomp_tableB.tex",	$estout_options_w_se order(incumbent_tenure logtenure pid_ce inc_mean_g1 inc_ll_95_g1)
estout  PreferredLowNoMean PreferredLowNoT5	 PreferredQ1LowNoT5	using "$texdir/uk_cbevcomp_tableC.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_ul_quin1_g1 inc_ll_95_g1)


probit vote_ce logtenure         inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoPID
probit vote_ce logtenure  inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowNoPID
probit vote_ce logtenure  inc_ul_quin1_g1   inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1LowNoPID
probit vote_ce logtenure  inc_mean_g1 inc_ul_quin1_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1LowNoPID
probit vote_ce logtenure  inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidNoPID
probit vote_ce logtenure  inc_median_g1   inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3MidNoPID
probit vote_ce logtenure  inc_mean_g1 inc_median_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3MidNoPID

*estout  BartelsLogtenure PreferredLow PreferredQ1Low MeanQ1Low PreferredMid PreferredQ3Mid MeanQ3Mid		using "dboutput/uk_cbevcomp_tableA.tex",	$estout_options order(logtenure pid_ce inc_mean_g1 inc_ul_quin1_g1 inc_ul_quin3_g1 inc_ll_95_g1)
estout  BartelsLogtenureNoPID PreferredLowNoPID PreferredQ1LowNoPID PreferredMidNoPID PreferredQ3MidNoPID		using "$texdir/uk_cbevcomp_tableD.tex",	$estout_options_w_se order(logtenure inc_mean_g1 inc_ul_quin1_g1 inc_median_g1 inc_ll_95_g1)


probit vote_ce logtenure pid_ce inc_ul_quin1_g1 inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store OwnMeanLow
probit vote_ce logtenure pid_ce inc_median_g1 inc_mean_g1 inc_ll_95_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store OwnMeanMid

estout  OwnMeanLow OwnMeanMid		using "$texdir/uk_cbevcomp_tableE.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_ul_quin1_g1 inc_median_g1 inc_ll_95_g1)



probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1 share5 share5_x_T5g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureS5
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1 share5 share5_x_T5g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowS5
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_g1 share5 share5_x_T5g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidS5

estout  BartelsLogtenureS5 PreferredLowS5 PreferredMidS5		using "$texdir/uk_cbevcomp_tableF.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 share5 inc_ll_95_g1 share5_x_T5g1)



gen model_country = "UK"
gen model_name = ""
gen predicted_diff_mean_m = .
gen predicted_diff_mean_se = .
gen predicted_diff_t5_m = .
gen predicted_diff_t5_se = .
local counter = 1
foreach model in BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid {
  estimates restore `model'
  summ inc_ll_95_g1 if e(sample)
  local t5_mean =  r(mean)
  local t5_sd   =  r(sd)
  local t5_mean_sd = `t5_mean' + `t5_sd'
  margins, pwcompare at((asobserved) _all inc_ll_95_g1 =(`t5_mean' `t5_mean_sd'))
  matrix pdiff_m = r(b_vs)
  matrix pdiff_v = r(V_vs)
  scalar pdiff_m = pdiff_m[1,1]
  scalar pdiff_se = pdiff_v[1,1]^(1/2)
  replace model_name = "`model'" if _n == `counter'
  replace predicted_diff_t5_m = pdiff_m if _n == `counter'
  replace predicted_diff_t5_se = pdiff_se if _n == `counter'
  // Now mean version
  if "`model'" == "BartelsLogtenure" | "`model'" == "PreferredLow" | "`model'" == "PreferredMid" {
    summ inc_mean_g1 if e(sample)
    local mean_mean =  r(mean)
    local mean_sd   =  r(sd)
    local mean_mean_sd = `mean_mean' + `t5_sd'
    margins, pwcompare at((asobserved) _all inc_mean_g1 =(`mean_mean' `mean_mean_sd'))
    matrix pdiff_m = r(b_vs)
    matrix pdiff_v = r(V_vs)
    scalar pdiff_m = pdiff_m[1,1]
    scalar pdiff_se = pdiff_v[1,1]^(1/2)
    replace model_name = "`model'" if _n == `counter'
    replace predicted_diff_mean_m = pdiff_m if _n == `counter'
    replace predicted_diff_mean_se = pdiff_se if _n == `counter'
  }
  // Increment counter
  local counter = `counter' + 1
}
keep model_country model_name predicted_diff_*
drop if model_name == ""
save "$gendatadir/uk_predicted_differences.dta", replace

